package cz.acies.genea.data.gedcom.tools;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.xml.sax.SAXParseException;

import cz.acies.genea.data.gedcom.model.EventFact;
import cz.acies.genea.data.gedcom.model.Gedcom;
import cz.acies.genea.data.gedcom.model.Visitor;
import cz.acies.genea.data.gedcom.parser.ModelParser;

import java.io.*;
import java.util.logging.Logger;

public class PlaceWriter extends Visitor
{
	private static final Logger logger = Logger.getLogger("org.folg.gedcom.tools");

	@Option(name = "-i", required = true, usage = "file or directory containing gedcom files to process")
	private File gedcomIn;

	@Option(name = "-o", required = true, usage = "target file for places")
	private File placesOut;

	private ModelParser parser;
	private PrintWriter out;

	public PlaceWriter() {
		parser = new ModelParser();
	}

	@Override
	public boolean visit(EventFact eventFact) {
		String place = eventFact.getPlace();
		if (place != null && place.length() > 0) {
			out.println(place);
		}
		return true;
	}

	public void processGedcom(File file) throws SAXParseException, IOException {
		Gedcom gedcom = parser.parseGedcom(file);
		gedcom.accept(this);
	}

	private void doMain() throws IOException, SAXParseException {
		out = new PrintWriter(new FileWriter(placesOut));
		if (gedcomIn.isDirectory()) {
			for (File file : gedcomIn.listFiles()) {
				processGedcom(file);
			}
		} else if (gedcomIn.isFile()) {
			processGedcom(gedcomIn);
		}
		out.close();
	}

	public static void main(String[] args) throws IOException, SAXParseException {
		PlaceWriter self = new PlaceWriter();
		CmdLineParser parser = new CmdLineParser(self);
		try {
			parser.parseArgument(args);
			self.doMain();
		} catch (CmdLineException e) {
			System.err.println(e.getMessage());
			parser.printUsage(System.err);
		}
	}
}
